package com.sky.service.impl;

import com.sky.Entity.Employee;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeQueryListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmplyeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {

        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();


        Employee employee = employeeMapper.selectByUserName(username);
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        String md5Pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!md5Pwd.equals(employee.getPassword())){
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus()== StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;
    }

    @Override
    public void add(EmployeeDTO employeeDTO) {
        Employee employee=new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);

//        private Integer status;
//        private String create_time;
//        private String update_time;
//        private Long create_user;
//        private Long update_user;
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        employee.setStatus(StatusConstant.ENABLE);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(BaseContext.getEmpId());
//        employee.setUpdateUser(BaseContext.getEmpId());
        int add = employeeMapper.add(employee);
    }

    @Override
    public EmployeeQueryListVo query(String name, Integer page, Integer pageSize) {

        //计算起始位置
        int startIndex=(page-1)*pageSize;
        if(startIndex<0){
            startIndex=0;
        }
        long total = employeeMapper.selectCount(name);
        List<Employee> employeeList = employeeMapper.query(name,startIndex, pageSize);
        EmployeeQueryListVo employeeQueryVo = EmployeeQueryListVo.builder()
                .total(total)
                .records(employeeList)
                .build();
        return employeeQueryVo;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Long empId = BaseContext.getEmpId();
        Employee employee=Employee.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(empId)
                .build();
        int result=employeeMapper.updateStatus(employee);
    }

    @Override
    public Employee queryById(Long id) {
        return employeeMapper.queryById(id);
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee=new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(BaseContext.getEmpId());
        employeeMapper.updateStatus(employee);
    }
}
